#include <stdio.h>
#include <string.h>
#define DEQ_DATA_TYPE char
#define NMAX          50000

/* {{{ DEQ Funcs */
#ifndef DEQ_DATA_TYPE
#define DEQ_DATA_TYPE int
#endif

#ifndef DEQUEUE_H
#define DEQUEUE_H

#ifndef NMAX
const int NMAX = 500000;
#endif

struct Dequeue {
	DEQ_DATA_TYPE data[NMAX];
	int head;
	int size;
};
#endif

extern void
Clear(struct Dequeue * d)
{
	d->size = 0;
	d->head = 0;
}



extern int
PushFront(struct Dequeue * d, int key)
{
	if (d->size < NMAX) {
		if (d->head == 0)
			d->head = NMAX-1;
		else
			--d->head;
		d->data[d->head] = key;
		d->size++;
		return 0; /* OK */
	} else {
		return -1; /* deq overflow */
	}
}



extern int
PushBack(struct Dequeue * d, int key)
{
	if (d->size < NMAX) {
		d->data[ (d->head + d->size) % NMAX ] = key;
		++d->size;
		return 0;
	} else {
		return -1; /* deq overflow */
	}
}



extern DEQ_DATA_TYPE
PopFront(struct Dequeue * d)
{
	if (d->size > 0) {
		--d->size;
		return d->data[(d->head)++];
	} else {
		return 0; /* do not touch head and size */
	}
}



extern DEQ_DATA_TYPE
PopBack(struct Dequeue * d)
{
/*	if (d->size) {
		--d->size;
		return d->data[ (d->head + d->size + 1) % NMAX ];
	}
	return 0; */
	return d->size ? d->data[ (d->head + (--d->size)) % NMAX ] : 0;
}



extern DEQ_DATA_TYPE
Front(const struct Dequeue * d)
{
	return d->data[d->head];
}



extern DEQ_DATA_TYPE
Back(const struct Dequeue * d)
{
	return d->data[(d->head + d->size - 1) % NMAX];
}



extern unsigned char
IsEmpty(const struct Dequeue * d)
{
	return d->size ? false : true;
}
/* }}} */


int
main(void)
{
	struct Dequeue beads;
	int n;
	register int i = 0;
	char c, new_c;

	(void) freopen("beads.in", "r", stdin);
	(void) freopen("beads.out","w",stdout);

	Clear(&beads);
	(void) scanf("%d\n", &n);
	while (i++ < n && (c = getchar()) != '\n') {
		PushBack(&beads, c);
	}
	PushBack(&beads, PopFront(&beads));
	while (c = getchar(), c != EOF) {
		if (c == '\n')
			continue;
		switch (c) {
			case 'D': /* D for delete */
				(void) PopFront(&beads);
				break;
			case 'I': /* I for insert */
				while (c = getchar(), c == ' ' || c == '\n');
				(void) PushFront(&beads, c);
				break;
			case 'M':
				(void) PushBack(&beads, PopFront(&beads));
				break;
		}
	}
	if (!IsEmpty(&beads)) {
		putchar(PopBack(&beads));
		while (!IsEmpty(&beads))
			putchar(PopFront(&beads));
	}
	putchar('\n');
	return 0;
}
// vim: foldmethod=marker:
